<template>
  <el-dialog
      v-model="deviceAddWinDialogVisible"
      title="新增设备类型"
      width="60%"
      @close="handleClose"
      draggable
      center
  >
    <template #default>
      <el-form :model="newDevice">
        <el-form-item label="设备类型编码">
          <el-input v-model="newDevice.deviceEncode"/>
        </el-form-item>
        <el-form-item label="父类型序号">
          <el-input v-model="newDevice.deviceParentId"/>
        </el-form-item>
        <el-form-item label="设备类型名称">
          <el-input v-model="newDevice.deviceName"/>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="newDevice.deviceStatus">
            <el-radio value="1" size="large">是</el-radio>
            <el-radio value="2" size="large">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="newDevice.deviceRemark"/>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="addDeviceSubmit">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ref, watch} from "vue";
import deviceRequest from "@/api/device.js";
// 保存新增项目表单的数据
let newDevice = ref({});
const props = defineProps(['flag']);
const emits = defineEmits(['cancelAdd','addComplete'])
let deviceAddWinDialogVisible = ref(false);

/**
 * 关闭窗体前的回调函数
 */
function handleClose(){
  newDevice = ref({});
  emits("cancelAdd");
}
function cancel(){
  newDevice = ref({});
  emits("cancelAdd");
}
/**
 * 新增设备的提交按钮事件
 */
function addDeviceSubmit() {
  if(newDevice.value.deviceParentId != null){
    newDevice.value.deviceParent = 2;
  }else{
    newDevice.value.deviceParent = 1;
  }
  deviceRequest.insertDevice(newDevice.value, res => {
    emits("addComplete");
    deviceAddWinDialogVisible.value = false;
  })
}

// 属性侦听
watch(() => props.flag, () => {
  deviceAddWinDialogVisible.value = props.flag === 1;
});
</script>
<style scoped>

</style>